package com.yxhl.km.mvp;

import android.util.Log;

import com.yxhl.km.MyApplication;
import com.yxhl.km.http.UrlBuilder;
import com.yxhl.km.http.cookie.CookiesInterceptor;
import com.yxhl.km.http.cookie.HttpCookieInterceptor;
import com.yxhl.km.http.imp.Logger;
import com.yxhl.km.http.retrofit.LoggingInterceptor;
import com.yxhl.km.http.retrofit.ResponseConverterFactory;
import com.yxhl.km.module.utils.NetworkUtil;
import com.yxhl.km.module.utils.ToastUtil;
import com.yxhl.km.module.view.DiagUtil;

import java.io.IOException;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by zhangyanan on 2017/11/16.
 */

public class BaseModel {
    public String getServerUrl() {
        return UrlBuilder.URL;
    }


    public <T> T buildServiceLogin(Class<T> service) {

        if (!NetworkUtil.isNetAvailable(MyApplication.mContext)) {
            ToastUtil.makeToast("当前网络不可用");
            return null;
        }
        LoggingInterceptor logging = new LoggingInterceptor(new Logger());
        logging.setLevel(LoggingInterceptor.Level.BODY);
        OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(new HttpCookieInterceptor())
                .addInterceptor(logging)
                .build();
        Retrofit retrofit = new Retrofit.Builder()
                .client(client)
                .baseUrl(getServerUrl())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
//                .addConverterFactory(ResponseConverterFactory.create())
                .build();
        return retrofit.create(service);
    }

    public <T> T buildService(Class<T> service) {
        if (!NetworkUtil.isNetAvailable(MyApplication.mContext)) {
            ToastUtil.makeToast("当前网络不可用");
            return null;
        }
        LoggingInterceptor logging = new LoggingInterceptor(new Logger());
        logging.setLevel(LoggingInterceptor.Level.BODY);
        OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(new CookiesInterceptor())
//                .addInterceptor(logging)
                .build();

        Retrofit retrofit = new Retrofit.Builder()
                .client(client)
                .baseUrl(getServerUrl())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        return retrofit.create(service);
    }
}
